home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12812 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: news.spies.com!usenet
  2. From: Erik Max Francis <max@alcyone.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help: 2 short functions
  5. Date: Tue, 02 Apr 1996 08:08:13 -0800
  6. Organization: Alcyone Systems
  7. Message-ID: <316150ED.1D0410A3@alcyone.com>
  8. References: <4ji734$n6v@masala.cc.uh.edu>
  9. NNTP-Posting-Host: newton.alcyone.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
  14.  
  15. odie garfield wrote:
  16.  
  17. > Hi, I have two functions that I'm not too sure about.
  18. > The first one:
  19. > char *get_filename(void)
  20. > {
  21. >    char string[20],*stringp;
  22. >    scanf("%s",string);
  23. >    stringp=&string[0];
  24. >    return stringp;
  25. > }
  26. > I'm want the function to read in a string and return a pointer to that string.
  27. > Am I doing it correctly?
  28.  
  29. No.  You are returning a pointer to an automatic (i.e., local) variable from a
  30. function.  This is a very bad thing.  The reason is because automatic variables
  31. no longer reliably exist outside of the scope of their block.  So you're
  32. returning a pointer to an area which may be reclaimed at any time.  Needless to
  33. say, this is a very bad thing.
  34.  
  35. It's especially bad because oftentimes, particularly if you're using the pointer
  36. right after you return from the function, it _seems_ to work.  It's just that
  37. every once in a while (especially on, say, a heavily-loaded multitasking system)
  38. you'll find that it crashes or causes strange behavior.
  39.  
  40. Your solutions are either to make the local array static (so it persists between
  41. calls), or to allocate memory for the array with malloc inside the function and
  42. return a pointer to that (but be sure to free it later on).
  43.  
  44. > My second function is related to the first function:
  45. > FILE *open_file(char *filenamep)
  46. > {
  47. >    FILE *fp;
  48. >    fp=fopen("??????","r");
  49. >    return fp;
  50. > }
  51. > In this 2nd function, I want to open a file using the pointer to a string I
  52. > got from the first function. But I don't know exactly how to do this. Any help
  53. > would be appreciated. Thanks.
  54.  
  55. This is fine.  The reason _this_ is fine is because fopen is filling out the
  56. FILE record that fp is pointing to for you (it actually calls malloc itself;
  57. fclose appropriately calls free).
  58.  
  59. Besides, if you look at it strictly, these are two different cases.  In the
  60. first function, you are declaring an automatic variable (char string[20]) and
  61. returning a pointer to it (char *stringp = string; return stringp).  In the
  62. second, you declaring an automatic variable (FILE *fp) and returning its value
  63. (return fp).  These are two different situation.  The first will get you into
  64. big trouble; the second will not.
  65.  
  66. -- 
  67. Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
  68. San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
  69. H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
  70. Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
  71. "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
  72.